package controller.business.inspectorAssessHistory;

import base.response.OffsetLimitPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import controller.common.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.InspectorAssessServices;
import services.business.InspectorInfoServices;
import wt.entity.business.*;

import java.util.*;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("business/inspectorAssessHistory")
public class InspectorAssessHistoryController extends BaseController {
    @Autowired
    InspectorAssessServices inspectorAssessServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;
    @Autowired
    BusinessInfoServices businessInfoServices;


    @RequiresPermissions("business:inspectorAssessHistory:default")
    @RequestMapping("index")
    public String index() throws Exception {
        return "business/inspectorAssessHistory/index";
    }

    @RequiresPermissions("business:inspectorAssessHistory:default")
    @ResponseBody
    @RequestMapping("select")
    public OffsetLimitPage select(
            String companyName,
            String productName,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            String toInspectorName,
            Integer offset,
            Integer limit
    ) throws Exception {
        PageHelper.offsetPage(offset, limit);
        List<InspectorAssess> inspectorAssessList = inspectorAssessServices.selectInspectorAssessHistory(startTime, endTime, companyName, productName, null, getLoginAccount().getExpertBase().getId(), toInspectorName);
        inspectorAssessServices.lazyLoadBusinessInfo(inspectorAssessList);
        Map<String, InspectorInfo> map = new HashMap<String, InspectorInfo>();
        for (InspectorAssess inspectorAssess : inspectorAssessList) {
            if (!map.containsKey(inspectorAssess.getToInspectorId())){
                map.put(inspectorAssess.getToInspectorId(), inspectorInfoServices.selectByPrimaryKey(inspectorAssess.getToInspectorId()));
            }
            inspectorAssess.setToInspectorInfo(map.get(inspectorAssess.getToInspectorId()));
        }
        return new OffsetLimitPage((Page) inspectorAssessList);
    }
}
